import QtQuick 2.0
import Qt3D 1.0
import Qt3D.Shapes 1.0
import "js/BlockInfo.js" as BI

Item3D{
    id: _root

    property int mode: 0        //0:通常, 1:輪っか
    property variant items: [_block111, _block112, _block113
        , _block121, _block122, _block123
        , _block131, _block132, _block133
        , _block211, _block212, _block213
        , _block221, _block222, _block223
        , _block231, _block232, _block233
        , _block311, _block312, _block313
        , _block321, _block322, _block323
        , _block331, _block332, _block333]

    property int textureMode: 0     //0:pastel 1:normal 2:curse

    property real rotate_y_angle: -45

    property alias freeAngleX: _rotate_select_1.angle
    property alias freeAngleY: _rotate_select_2.angle
    property alias freeAngleZ: _rotate_select_3.angle

    property bool runnning: _block111.running | _block112.running | _block113.running
                            | _block121.running | _block122.running | _block123.running
                            | _block131.running | _block132.running | _block133.running
                            | _block211.running | _block212.running | _block213.running
                            | _block221.running | _block222.running | _block223.running
                            | _block231.running | _block232.running | _block233.running
                            | _block311.running | _block312.running | _block313.running
                            | _block321.running | _block322.running | _block323.running
                            | _block331.running | _block332.running | _block333.running
    transform: [
        Rotation3DforBlock {
            id: _rotate_select_1
            angle: 0
            axis: Qt.vector3d(1, 0, 0)
            duration: 500
        },
        Rotation3DforBlock {
            id: _rotate_select_2
            angle: -90
            axis: Qt.vector3d(0, 1, 0)
            duration: 500
        },
        Rotation3DforBlock {
            id: _rotate_select_3
            angle: -45
            axis: Qt.vector3d(0, 0, 1)
            duration: 500
        },
        Rotation3DforBlock {
            id: _rotate_y
            angle: rotate_y_angle
            axis: Qt.vector3d(0, 1, 0)
            origin: Qt.vector3d(-_root.x, -_root.y, -_root.z)
        }
    ]

    NumberAnimation {
        id: _loopAnim
        running: false
        loops: Animation.Infinite
        target: _rotate_y
        property: "angle"
        duration: 60000//20000
        from: 0
        to: 360
    }

    Behavior on x{
        NumberAnimation{ id: _animPosX; duration: 200  }
    }
    Behavior on y{
        NumberAnimation{ id: _animPosY; duration: 200; }
    }
    Behavior on z{
        NumberAnimation{ id: _animPosZ; duration: 200  }
    }


    //Bottom
    Block{ id: _block111; x: -1; y: -1; z: -1; textureMode: _root.textureMode; color: "#000111" }   //South     //East
    Block{ id: _block112; x:  0; y: -1; z: -1; textureMode: _root.textureMode; color: "#000112" }   //South     //Center
    Block{ id: _block113; x:  1; y: -1; z: -1; textureMode: _root.textureMode; color: "#000113" }   //South     //West
    Block{ id: _block121; x: -1; y: -1; z:  0; textureMode: _root.textureMode; color: "#000121" }   //Center
    Block{ id: _block122; x:  0; y: -1; z:  0; textureMode: _root.textureMode; color: "#000122" }   //Center
    Block{ id: _block123; x:  1; y: -1; z:  0; textureMode: _root.textureMode; color: "#000123" }   //Center
    Block{ id: _block131; x: -1; y: -1; z:  1; textureMode: _root.textureMode; color: "#000131" }   //North
    Block{ id: _block132; x:  0; y: -1; z:  1; textureMode: _root.textureMode; color: "#000132" }   //North
    Block{ id: _block133; x:  1; y: -1; z:  1; textureMode: _root.textureMode; color: "#000133" }   //North

    //Center
    Block{ id: _block211; x: -1; y:  0; z: -1; textureMode: _root.textureMode; color: "#000211" }
    Block{ id: _block212; x:  0; y:  0; z: -1; textureMode: _root.textureMode; color: "#000212" }
    Block{ id: _block213; x:  1; y:  0; z: -1; textureMode: _root.textureMode; color: "#000213" }
    Block{ id: _block221; x: -1; y:  0; z:  0; textureMode: _root.textureMode; color: "#000221" }
    Block{ id: _block222; x:  0; y:  0; z:  0; textureMode: _root.textureMode; color: "#000222" }
    Block{ id: _block223; x:  1; y:  0; z:  0; textureMode: _root.textureMode; color: "#000223" }
    Block{ id: _block231; x: -1; y:  0; z:  1; textureMode: _root.textureMode; color: "#000231" }
    Block{ id: _block232; x:  0; y:  0; z:  1; textureMode: _root.textureMode; color: "#000232" }
    Block{ id: _block233; x:  1; y:  0; z:  1; textureMode: _root.textureMode; color: "#000233" }

    //Top
    Block{ id: _block311; x: -1; y:  1; z: -1; textureMode: _root.textureMode; color: "#000311" }
    Block{ id: _block312; x:  0; y:  1; z: -1; textureMode: _root.textureMode; color: "#000312" }
    Block{ id: _block313; x:  1; y:  1; z: -1; textureMode: _root.textureMode; color: "#000313" }
    Block{ id: _block321; x: -1; y:  1; z:  0; textureMode: _root.textureMode; color: "#000321" }
    Block{ id: _block322; x:  0; y:  1; z:  0; textureMode: _root.textureMode; color: "#000322" }
    Block{ id: _block323; x:  1; y:  1; z:  0; textureMode: _root.textureMode; color: "#000323" }
    Block{ id: _block331; x: -1; y:  1; z:  1; textureMode: _root.textureMode; color: "#000331" }
    Block{ id: _block332; x:  0; y:  1; z:  1; textureMode: _root.textureMode; color: "#000332" }
    Block{ id: _block333; x:  1; y:  1; z:  1; textureMode: _root.textureMode; color: "#000333" }


    //選択カーソル用リング
    SelectRing{
        id: _ring
        enabled: true
    }


    //キーイベント処理
    function keyEvent(event){
        if(event.modifiers === Qt.ControlModifier){
            switch(event.key){
            case Qt.Key_Up:
//                freeAngleX += 90;
                break;
            case Qt.Key_Down:
//                freeAngleX -= 90;
                break;
            case Qt.Key_Left:
                rotate_y_angle += 90;
//                _blocks.freeAngleY += 90;
                break;
            case Qt.Key_Right:
                rotate_y_angle -= 90;
//                _blocks.freeAngleY -= 90;
                break;

            case Qt.Key_Escape:
                resetRotate();
                break;
            default:
                break;
            }
        }else{
            switch(event.key){
            case Qt.Key_Up:
                moveSelector(true);
                break;
            case Qt.Key_Down:
                moveSelector(false);
                break;
            case Qt.Key_Left:
                moveSelector(false);
                break;
            case Qt.Key_Right:
                moveSelector(true);
                break;
            case Qt.Key_Z:
                rotateSelectedBlock(false);
                break;
            case Qt.Key_Space:
                rotateSelectedBlock(true);
                break;

            case Qt.Key_Escape:
                resetRotate();
                break;
            default:
                break;
            }
        }
        var i=0;
    }

    //操作用の回転をクリアする
    function resetRotate(){
        _rotate_select_1.angle = 0;
        _rotate_select_2.angle = -90;
        _rotate_select_3.angle = -45;
        rotate_y_angle = -45;
    }

    //キューブの回転をクリアする
    function resetRotateBlock(){
        for(var i=0; i<_root.items.length; i++){
            _root.items.clearTransform();
        }
    }


    //リングを移動する
    function moveSelector(isPlus){
        _ring.moveSelector(isPlus);
    }

    //キューブを回転する（選択中）
    function rotateSelectedBlock(isPlus){
        rotateBlock(_ring.position, isPlus);
    }

    //キューブを回転する
    function rotateBlock(position, isPlus){
        var i=0;
        var index = 0;
        var indexs = [];
        var axis = Qt.vector3d(0, 1, 0);

        //回転するブロックを選ぶ
        indexs = getBlocks(position);
        switch(position){
        case 0:
            //Top
            axis = Qt.vector3d(0, 1, 0);
            break;
        case 1:
            //Bottom
            axis = Qt.vector3d(0, 1, 0);
            break;
        case 2:
            //North
            axis = Qt.vector3d(0, 0, 1);
            break;
        case 3:
            //South
            axis = Qt.vector3d(0, 0, 1);
            break;
        case 4:
            //West
            axis = Qt.vector3d(1, 0, 0);
            break;
        case 5:
            //East
            axis = Qt.vector3d(1, 0, 0);
            break;
        default:
            break;
        }

        if(indexs.length != 9){
            console.debug("CHK:id=" + position + ", len=" + indexs.length + ", axis=" + axis.x + ", " + axis.y + ", " + axis.z);
        }
        for(i=0; i<indexs.length; i++){
            index = indexs[i];

            if(indexs.length != 9){
                var b = _root.items[index];
                var v = b.realPos();
                var k = 1;
                console.debug("CHK:" + index + ":" + b.color
                              + ":" + v.x + ", " + v.y + ", " + v.z);
                console.debug("  " + k
                              + " / " + b.transform[k].translate.x + ", " + b.transform[k].translate.y + ", " + b.transform[k].translate.z
                              );
                for(k=2; k<b.transform.length; k++){
                    console.debug("  " + k + ":" + b.transform[k].angle
                                  + " / " + b.transform[k].axis.x + ", " + b.transform[k].axis.y + ", " + b.transform[k].axis.z
                                  + " / " + b.transform[k].origin.x + ", " + b.transform[k].origin.y + ", " + b.transform[k].origin.z
                                  + " / " + b.transform[k].isSpin
                                  );
                }
            }

            if(axis.x == 1){
                _root.items[index].rotateX(isPlus);
            }else if(axis.y == 1){
                _root.items[index].rotateY(isPlus);
            }else if(axis.z == 1){
                _root.items[index].rotateZ(isPlus);
            }else{

            }
        }
    }

    //回転するブロックを選ぶ
    //position 0:Top, 1:Bottom, 2:North, 3:South, 4:West, 5:East
    function getBlocks(position){
        var indexs = [];
        var pos = Qt.vector3d(0, 0, 0);
        for(var i=0; i<_root.items.length; i++){
            pos = _root.items[i].realPos();
            switch(position){
            case 0:
                //Top
                if(pos.y === 1){
                    indexs.push(i);
                }
                break;
            case 1:
                //Bottom
                if(pos.y === -1){
                    indexs.push(i);
                }
                break;
            case 2:
                //North
                if(pos.z === 1){
                    indexs.push(i);
                }
                break;
            case 3:
                //South
                if(pos.z === -1){
                    indexs.push(i);
                }
                break;
            case 4:
                //West
                if(pos.x === 1){
                    indexs.push(i);
                }
                break;
            case 5:
                //East
                if(pos.x === -1){
                    indexs.push(i);
                }
                break;
            default:
                break;
            }
        }
        return indexs;
    }

    //キューブが元の位置にいるか
    function checkBlocksPosition(position){
        var ret = true;
        var pos = Qt.vector3d(0, 0, 0);
        for(var i=0; i<_root.items.length; i++){
            pos = _root.items[i].realPos();
            if(_root.items[i].position === pos){
                //ok
            }else{
                ret = false;
            }
        }
        return ret;
    }

    function outputVectorDebug(header, data){
        console.debug(header + data.x + ", " + data.y + ", " + data.z);
    }


    StateGroup{
        id: _scene
        states:[
            State{
                name: "loop"
                when: mode == 1
                PropertyChanges { target: _block111; x: BI.R * Math.cos(2 * Math.PI * 1 / 27); z: BI.R * Math.sin(2 * Math.PI * 1 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block112; x: BI.R * Math.cos(2 * Math.PI * 2 / 27); z: BI.R * Math.sin(2 * Math.PI * 2 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block113; x: BI.R * Math.cos(2 * Math.PI * 3 / 27); z: BI.R * Math.sin(2 * Math.PI * 3 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block121; x: BI.R * Math.cos(2 * Math.PI * 4 / 27); z: BI.R * Math.sin(2 * Math.PI * 4 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block122; x: BI.R * Math.cos(2 * Math.PI * 5 / 27); z: BI.R * Math.sin(2 * Math.PI * 5 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block123; x: BI.R * Math.cos(2 * Math.PI * 6 / 27); z: BI.R * Math.sin(2 * Math.PI * 6 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block131; x: BI.R * Math.cos(2 * Math.PI * 7 / 27); z: BI.R * Math.sin(2 * Math.PI * 7 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block132; x: BI.R * Math.cos(2 * Math.PI * 8 / 27); z: BI.R * Math.sin(2 * Math.PI * 8 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block133; x: BI.R * Math.cos(2 * Math.PI * 9 / 27); z: BI.R * Math.sin(2 * Math.PI * 9 / 27); y: 0; scale: BI.S }

                PropertyChanges { target: _block211; x: BI.R * Math.cos(2 * Math.PI * 10 / 27); z: BI.R * Math.sin(2 * Math.PI * 10 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block212; x: BI.R * Math.cos(2 * Math.PI * 11 / 27); z: BI.R * Math.sin(2 * Math.PI * 11 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block213; x: BI.R * Math.cos(2 * Math.PI * 12 / 27); z: BI.R * Math.sin(2 * Math.PI * 12 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block221; x: BI.R * Math.cos(2 * Math.PI * 13 / 27); z: BI.R * Math.sin(2 * Math.PI * 13 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block222; x: BI.R * Math.cos(2 * Math.PI * 14 / 27); z: BI.R * Math.sin(2 * Math.PI * 14 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block223; x: BI.R * Math.cos(2 * Math.PI * 15 / 27); z: BI.R * Math.sin(2 * Math.PI * 15 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block231; x: BI.R * Math.cos(2 * Math.PI * 16 / 27); z: BI.R * Math.sin(2 * Math.PI * 16 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block232; x: BI.R * Math.cos(2 * Math.PI * 17 / 27); z: BI.R * Math.sin(2 * Math.PI * 17 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block233; x: BI.R * Math.cos(2 * Math.PI * 18 / 27); z: BI.R * Math.sin(2 * Math.PI * 18 / 27); y: 0; scale: BI.S }

                PropertyChanges { target: _block311; x: BI.R * Math.cos(2 * Math.PI * 19 / 27); z: BI.R * Math.sin(2 * Math.PI * 19 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block312; x: BI.R * Math.cos(2 * Math.PI * 20 / 27); z: BI.R * Math.sin(2 * Math.PI * 20 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block313; x: BI.R * Math.cos(2 * Math.PI * 21 / 27); z: BI.R * Math.sin(2 * Math.PI * 21 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block321; x: BI.R * Math.cos(2 * Math.PI * 22 / 27); z: BI.R * Math.sin(2 * Math.PI * 22 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block322; x: BI.R * Math.cos(2 * Math.PI * 23 / 27); z: BI.R * Math.sin(2 * Math.PI * 23 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block323; x: BI.R * Math.cos(2 * Math.PI * 24 / 27); z: BI.R * Math.sin(2 * Math.PI * 24 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block331; x: BI.R * Math.cos(2 * Math.PI * 25 / 27); z: BI.R * Math.sin(2 * Math.PI * 25 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block332; x: BI.R * Math.cos(2 * Math.PI * 26 / 27); z: BI.R * Math.sin(2 * Math.PI * 26 / 27); y: 0; scale: BI.S }
                PropertyChanges { target: _block333; x: BI.R * Math.cos(2 * Math.PI * 27 / 27); z: BI.R * Math.sin(2 * Math.PI * 27 / 27); y: 0; scale: BI.S }

                //回転
                PropertyChanges {
                    target: _loopAnim
                    running: true
                    duration: 20000
                }

                //セレクタ
                PropertyChanges {
                    target: _ring
                    enabled: false
                }

                //フリー回転
                PropertyChanges { target: _rotate_y; angle: 0;   axis: Qt.vector3d(0.2, 1, 0) }

                //遊ぶ時の向き変えのリセット
                PropertyChanges { target: _rotate_select_1; angle: 0;   axis: Qt.vector3d(1, 0, 0) }
                PropertyChanges { target: _rotate_select_2; angle: 0;   axis: Qt.vector3d(0, 1, 0) }
                PropertyChanges { target: _rotate_select_3; angle: 0;   axis: Qt.vector3d(0, 0, 1) }
            },
            //遊ぶ時の向き変え
            State{
                name: "Top"
                when: _ring.position == 0 || _ring.position == 3
                PropertyChanges { target: _rotate_select_1; angle: 0;   axis: Qt.vector3d(1, 0, 0) }
                PropertyChanges { target: _rotate_select_2; angle: -90;   axis: Qt.vector3d(0, 1, 0) }
                PropertyChanges { target: _rotate_select_3; angle: -45;   axis: Qt.vector3d(0, 0, 1) }
//            },
//            State{
//                //bottom west
//                name: "Bottom"
//                when: _ring.position == 1 || _ring.position == 4
//                PropertyChanges { target: _rotate_select_1; angle: 0;   axis: Qt.vector3d(1, 0, 0) }
//                PropertyChanges { target: _rotate_select_2; angle: 0;   axis: Qt.vector3d(0, 1, 0) }
//                PropertyChanges { target: _rotate_select_3; angle: 45;   axis: Qt.vector3d(0, 0, 1) }
//            },
//            State{
//                name: "North"
//                when: _ring.position == 2 || _ring.position == 5
//                PropertyChanges { target: _rotate_select_1; angle: -90;   axis: Qt.vector3d(1, 0, 0) }
//                PropertyChanges { target: _rotate_select_2; angle: 180;   axis: Qt.vector3d(0, 1, 0) }
//                PropertyChanges { target: _rotate_select_3; angle: -45;   axis: Qt.vector3d(0, 0, 1) }
//            },
//            State{
//                name: "South"
//                when: _ring.position == 3
//                PropertyChanges { target: _rotate_select_1; angle: 90;   axis: Qt.vector3d(1, 0, 0) }
//                PropertyChanges { target: _rotate_select_2; angle: -45;   axis: Qt.vector3d(0, 0, 1) }
//                PropertyChanges { target: _rotate_select_3; angle: -45;   axis: Qt.vector3d(0, 1, 0) }
//            },
//            State{
//                name: "West"
//                when: _ring.position == 4
//                PropertyChanges { target: _rotate_select_1; angle: 0;   axis: Qt.vector3d(1, 0, 0) }
//                PropertyChanges { target: _rotate_select_2; angle: 45;   axis: Qt.vector3d(0, 0, 1) }
//                PropertyChanges { target: _rotate_select_3; angle: -45;   axis: Qt.vector3d(0, 1, 0) }
//            },
//            State{
//                name: "East"
//                when: _ring.position == 5
//                PropertyChanges { target: _rotate_select_1; angle: 0;   axis: Qt.vector3d(1, 0, 0) }
//                PropertyChanges { target: _rotate_select_2; angle: -135;   axis: Qt.vector3d(0, 0, 1) }
//                PropertyChanges { target: _rotate_select_3; angle: -45;   axis: Qt.vector3d(0, 1, 0) }
            }

        ]
    }

}
